<?php

namespace Home\Controller;

use Think\Controller;

class NoticController extends Controller {

    public function index() {
        $this->display("UserNotic");
    }

    /**
     * 发送消息
     * 群发的消息类型，图文消息为mpnews，文本消息为text，语音为voice，音乐为music，图片为image，视频为video
     */
    public function SentMessage() {
        $wechatHelper = new WechatHelperController();
        $sendType = I('sendType', '', 'htmlspecialchars');
        $openIDArray = null;
        if ($sendType == '2') {
            $data = $wechatHelper->get_user_list();
            $openIDArray = $data['data']['openid'];
        }
        $groupID = I('groupId', '', 'htmlspecialchars');
        $messageType = I('messageType', '', 'htmlspecialchars');
        $content = I('message', '', 'htmlspecialchars');
        $noticeID = I('noticeID', '', 'htmlspecialchars');
        switch ($messageType) {
            case 'text':
                $noticeID = $this->SaveTextMessage($groupID, $content);
                break;
            case 'image':
                $result = $wechatHelper->upload_media('image', $content);
                $content = $result['media_id'];
                break;
            case 'voice':
                $result = $wechatHelper->upload_media('voice', $content);
                $content = $result['media_id'];
                break;
            case 'mpvideo':
                $upload_result = $wechatHelper->upload_media('video', $content);
                $media_id = $upload_result['media_id'];
                $title = I('title', '', 'htmlspecialchars');
                $description = I('description', '', 'htmlspecialchars');
                $result = $wechatHelper->upload_video($media_id, $title, $description);
                $content = $result['media_id'];
                break;
            case 'mpnews'://像语音一样
                $thumb_array = $wechatHelper->upload_media('image', $content);
                $thumb_media_id = $thumb_array["media_id"];
//                echo "thumb_media_id = " . $thumb_media_id;
                $author = I('author', '', 'htmlspecialchars');
                $title = I('title', '', 'htmlspecialchars');
                $content_source_url = I('content_source_url', '', 'htmlspecialchars');
                $contents = I('contents', '', 'htmlspecialchars');
                $digest = I('digest', '', 'htmlspecialchars');
                $show_cover_pic = I('show_cover_pic', '', 'htmlspecialchars');

                /**
                 * 判断标题和正文内容是否为空
                 */
                if ($title == null || $title == "") {//标题为空
                    $this->ajaxReturn('标题不能为空', 'EVAL');
                    return;
                } else if ($contents == null || $contents == "") {//正文内容为空
                    $this->ajaxReturn('正文内容不能为空', 'EVAL');
                    return;
                }

//                echo " 这里是前台的消息 start==>author=".$author." content_source_url=".$content_source_url." contents= ".$contents." digest= ".$digest." show_cover_pic =".$show_cover_pic."<==end";
                /**
                 * 以下是处理mpnews的data数据
                 */
                $news[] = array("thumb_media_id" => $thumb_media_id,
                    "author" => $author,
                    "title" => $title,
                    "content_source_url" => $content_source_url,
                    "content" => $contents,
                    "digest" => $digest,
                    "show_cover_pic" => $show_cover_pic
                );
                $datanews = array("articles" => $news);
                /**
                 * mpnews处理完成
                 */
                $upload_result = $wechatHelper->upload_mpnews($datanews);
//                echo "这里是jsondecode的返回: ";
//                print_r($upload_result);
                $content = $upload_result['media_id'];
                break;
            case "mulmpnews":
                $message = I('message', '', 'htmlspecialchars');
//                print_r($message);
                $thumb_media_id;
                $title;
                $author;
                $contents;
                $show_cover_pic;
                $content_source_url;
                $mpnewsFile;
                $tag = 1;
                foreach ($message as $item) {
                    foreach ($item as $key => $value) {
                        switch ($key) {
                            case "mpnewstitle":
                                $title = $value;
                                if ($title == null || $title == "") {//标题为空
                                    $this->ajaxReturn('标题不能为空', 'EVAL');
                                    return;
                                }
                                break;
                            case "mpnewsauthor":
                                $author = $value;
                                break;
                            case "mpnewsmainbody":
                                $contents = $value;
                                if ($contents == null || $contents == "") {//正文内容为空
                                    $this->ajaxReturn('正文内容不能为空', 'EVAL');
                                    return;
                                }
                                break;
                            case "mpnewsifshow":
                                $show_cover_pic = $value;
                                break;
                            case "mpnewsSrcurl":
                                $content_source_url = $value;
                                break;
                            case "mpnewsFile":
                                $mpnewsFile = $value;
                                break;
                        }
                    }
                    $thumb_array = $wechatHelper->upload_media('image', $mpnewsFile);
                    $thumb_media_id = $thumb_array["media_id"];
//                    echo " Tag = " . $tag++ . "-->thumb_media_id = " . $thumb_media_id;
//                    echo " content = ".$contents;
                    $news[] = array("thumb_media_id" => $thumb_media_id,
                        "author" => $author,
                        "title" => $title,
                        "content_source_url" => $content_source_url,
                        "content" => $contents,
                        "show_cover_pic" => $show_cover_pic
                    );
                }
                $datanews = array("articles" => $news);
                /**
                 * mpnews处理完成
                 */
                $upload_result = $wechatHelper->upload_mpnews($datanews);
//                echo "这里是jsondecode的返回: ";
//                print_r($upload_result);
                $content = $upload_result['media_id'];
                break;
            default:
                break;
        }

        $data = $this->getMessageJSON($messageType, $sendType, $groupID, $openIDArray, $content);
//        $result = $wechatHelper->Send_Message($sendType, $data);
//        echo " errcode = " . $result["errcode"];
        if ($result["errcode"] == 0) {
            $this->SaveSendMessageInfo($messageType, $sendType, $groupID, $noticeID, $result["msg_id"]);
            $this->ajaxReturn('y', 'EVAL');
        } else {
            $this->ajaxReturn('n', 'EVAL');
        }
    }

    // <editor-fold defaultstate="collapsed" desc="文本消息">

    /**
     * 保存文本消息
     * @param type $wechatGroupId 发送文本消息的微信组ID，发送全部成员时该字段为空
     * @param type $content 文本消息内容
     */
    private function SaveTextMessage($wechatGroupId, $content) {
        $noticeText = M('notictext');
        $data['MessageText'] = $content;
        $data['InsertTime'] = time();
        $data['PublicCodeID'] = 1;
        $data['MsgSource'] = 1;
        $noticId = $noticeText->add($data);
        return $noticId;
    }

    // </editor-fold>
    // <editor-fold defaultstate="collapsed" desc="保存发送列表">
    /**
     * 保存发送出去的消息列表
     * @param type $messageType 消息类型
     * @param type $sendType 发送类型，1为按组发送，2为发送所有成员
     * @param type $WeChatGroupId 发送组的ID （后面需要改成前台判断传空或非空）
     * @param type $NoticId 消息对应的数据库中的ID
     * @param type $MsgId 发送提交成功后微信返回的消息ID
     */
    private function SaveSendMessageInfo($messageType, $sendType, $WeChatGroupId, $NoticId, $MsgId) {
        $sendInfo = M('hadsendinfo');
        switch ($messageType) {
            case 'text':
                $sendInfoData['NoticType'] = 1;
                break;
            case 'image':
                $sendInfoData['NoticType'] = 2;
                break;
            case 'voice':
                $sendInfoData['NoticType'] = 3;
                break;
            case 'graphic':
                $sendInfoData['NoticType'] = 5;
                break;
            default:
                break;
        }
        if ($sendType != '2') {
            $sendInfoData['WeChatGroupId'] = $WeChatGroupId;
        } else {
            $sendInfoData['WeChatGroupId'] = '';
        }

        $sendInfoData['NoticId'] = $NoticId;
        $sendInfoData['SendTime'] = time();
        $sendInfoData['PublicCodeID'] = 1;
        $sendInfoData['MsgId'] = $MsgId;
        $sendInfo->add($sendInfoData);
    }

    // </editor-fold>

    /**
     * 获取群发消息的JSON 
     * @param string $messageType 消息类型
     * @param string $sendType 发送类型 1.按分组群发 2 群发所有用户
     * @param string $groupID  分组ID
     * @param array  $openIDArray  所有用户的openID数组
     * @param string $content  文本内容
     */
    private function getMessageJSON($messageType, $sendType, $groupID = '', $openIDArray, $content) {
        $array = array();
        switch ($sendType) {
            case '1':
                $array['filter'] = array('group_id' => $groupID);
                break;
            case '2':
                $array['touser'] = $openIDArray;
                break;
            default:
                break;
        }
        switch ($messageType) {
            case 'text':
                $array['text'] = array('content' => $content);
                $array['msgtype'] = 'text';
                break;
            case 'image':
                $array['image'] = array('media_id' => $content);
                $array['msgtype'] = 'image';
                break;
            case 'voice':
                $array['voice'] = array('media_id' => $content);
                $array['msgtype'] = 'voice';
                break;
            case 'mpvideo':
                $array['mpvideo'] = array('media_id' => $content);
                $array['msgtype'] = 'mpvideo';
                break;
            case 'mpnews':
                $array['mpnews'] = array('media_id' => $content);
                $array['msgtype'] = 'mpnews';
                break;
            case 'mulmpnews':
                $array['mpnews'] = array('media_id' => $content);
                $array['msgtype'] = 'mpnews';
                break;
        }
//        echo "这里是getMessage返回的：=》";
//        print_r($array);
//        echo "mpnews = ".$array["mpnews"];
//        echo "msgtype".$array["msgtype"];
        $data = json_encode($array, JSON_UNESCAPED_UNICODE);
        return $data;
    }

    /**
     * 修改单图文图片信息
     */
    public function UpdateFile() {
        $mpid = I('mpid', '', 'htmlspecialchars');
        $saveId = I('saveId', '', 'htmlspecialchars');
        $filename;
        if ($mpid > 0) {
            $filename = "mpnewsFile" . $mpid;
        } else {
            $filename = 'graphicFile';
        }
        $upload = new \Think\Upload(); // 实例化上传类
        $upload->savePath = ''; // 设置附件上传（子）目录
        $upload->maxSize = 2097152; // 设置附件上传大小
        $upload->rootPath = './Uploads/Graphics/'; // 设置附件上传根目录
        $upload->exts = array('bmp', 'png', 'jpeg', 'jpg', 'gif');
        $info = $upload->uploadOne($_FILES[$filename]);

        if (!$info) {// 上传错误提示错误信息
            $str = $upload->getError();
            $result = Array("error" => $filename, "errstr" => $str);
        } else {// 上传成功 获取上传文件信息
            $Graphic = M('noticegraphic');
            $filePath = "Uploads/Graphics/" . $info['savepath'] . $info['savename'];
            $data = array("CoverPath" => $filePath, "ID" => $saveId);
            $addID = $Graphic->save($data);
            $result = array("error" => 0, "saveId" => $saveId, "imgpath" => $filePath);
        }
        $data = json_encode($result, JSON_UNESCAPED_UNICODE);
        $this->ajaxReturn($data, 'EVAL');
    }

    /**
     * 上传文件统一入口
     */
    public function UploadFile() {
        $fileType = I('fileType', '', 'htmlspecialchars');
        $upload = new \Think\Upload(); // 实例化上传类

        $upload->savePath = ''; // 设置附件上传（子）目录
        $info;
        switch ($fileType) {
            case 'image':
                $upload->maxSize = 2097152; // 设置附件上传大小
                $upload->rootPath = './Uploads/Images/'; // 设置附件上传根目录
                $upload->exts = array('bmp', 'png', 'jpeg', 'jpg', 'gif'); // 设置附件上传类型
                $info = $upload->uploadOne($_FILES['imageFile']);
                break;
            case 'voice':
                $upload->maxSize = 2097152; // 设置附件上传大小
                $upload->rootPath = './Uploads/Voices/'; // 设置附件上传根目录
                $upload->exts = array('mp3', 'amr');
                $info = $upload->uploadOne($_FILES['voiceFile']);
                break;
            case 'mpvideo':
                $upload->maxSize = 20971520; // 设置附件上传大小
                $upload->rootPath = './Uploads/Videos/'; // 设置附件上传根目录
                $upload->exts = array('mp4', 'rm', 'rmvb', 'wmv', 'avi', 'mpg', 'mpeg');
                $info = $upload->uploadOne($_FILES['videoFile']);
                //视频上传时需要标题和内容，所以此处在上传返回的info中添加这两个字段，方便存入数据库
//                $info['title'] = I('title', '', 'htmlspecialchars');
//                $info['description'] = I('description', '', 'htmlspecialchars');
                break;
            case "mpnews":
                $upload->maxSize = 2097152; // 设置附件上传大小
                $upload->rootPath = './Uploads/Graphics/'; // 设置附件上传根目录
                $upload->exts = array('bmp', 'png', 'jpeg', 'jpg', 'gif');
                $info = $upload->uploadOne($_FILES['graphicFile']);
                break;
            case "mulmpnews":
                $mpid = I('mpid', '', 'htmlspecialchars');
                $filename = "mpnewsFile" . $mpid; //拼接文件名
                $upload->maxSize = 2097152; // 设置附件上传大小
                $upload->rootPath = './Uploads/Graphics/'; // 设置附件上传根目录
                $upload->exts = array('bmp', 'png', 'jpeg', 'jpg', 'gif');
                $info = $upload->uploadOne($_FILES[$filename]);
                break;
            default:
                break;
        }
        if (!$info) {// 上传错误提示错误信息
            $str = $upload->getError();
            $save_result = Array("error" => 1, "errstr" => $str);
            $this->ajaxReturn($str, 'EVAL');
        } else {// 上传成功 获取上传文件信息
            $ID = $this->SaveInfo($fileType, $info);
            $save_result = Array("error" => 0, "saveId" => $ID["ID"], "imgpath" => $ID["imgpath"]);
            $data = json_encode($save_result, JSON_UNESCAPED_UNICODE);
            $this->ajaxReturn($data, 'EVAL');
        }
    }

    /**
     * 根据类型保存数据库
     * @param type $fileType 文件类型
     * @param type $info 上传结果
     */
    private function SaveInfo($fileType, $info) {
        $id;
        switch ($fileType) {
            case 'image':
                $id = $this->SaveImageInfo($info);
                break;
            case 'voice':
                $id = $this->SaveVoiceInfo($info);
                break;
            case 'mpvideo':
                $id = $this->SaveVideoInfo($info);
                break;
            case 'mpnews':
                $id = $this->SaveGraphicimgInfo($info);
                break;
            case 'mulmpnews':
                $id = $this->SaveGraphicimgInfo($info);
                break;
            default:
                break;
        }
        return $id;
    }

    // <editor-fold defaultstate="collapsed" desc="图片消息">
    /**
     * 保存图片上传信息至数据库
     * @param type $info 图片上传结果
     */
    private function SaveImageInfo($info) {
        $Image = M('noticimage');
        $ImageData['ImageName'] = $info['name'];
        $ImageData['ImagePath'] = "Uploads/Images/" . $info['savepath'] . $info['savename'];
        $ImageData['ImageSize'] = $info['size'] / 1024;
        $ImageData['UploadTime'] = time();
        $ImageData['PublicCodeID'] = 1;
        $result = $Image->add($ImageData);
        return $result;
    }

    /**
     * 获取上传的图片列表
     */
    public function getImageList() {
        $Image = M('noticimage');
        $condition['PublicCodeID'] = 1;
        $imageArray = $Image->where($condition)->select();
        if (count($imageArray) == 0) {
            $imageArray = array();
        } else {
            for ($index = 0; $index < count($imageArray); $index++) {
                $imageArray[$index]['UploadTime'] = date('Y-m-d H:i:s', $imageArray[$index]['UploadTime']);
            }
        }
        $json = json_encode($imageArray, JSON_UNESCAPED_UNICODE);
        $this->ajaxReturn($json, 'EVAL');
    }

    // </editor-fold>
    // <editor-fold defaultstate="collapsed" desc="语音消息">
    /**
     * 保存语音上传信息至数据库
     * @param type $info 语音上传结果
     */
    private function SaveVoiceInfo($info) {
        $Image = M('noticvoice');
        $ImageData['VoiceName'] = $info['name'];
        $ImageData['VoicePath'] = "Uploads/Voices/" . $info['savepath'] . $info['savename'];
        $ImageData['VoiceSize'] = $info['size'] / 1024;
        $ImageData['UploadTime'] = time();
        $ImageData['PublicCodeID'] = 1;
        $result = $Image->add($ImageData);
        return $result;
    }

    /**
     * 获取上传的语音列表
     */
    public function getVoiceList() {
        $Voice = M('noticvoice');
        $condition['PublicCodeID'] = 1;
        $voiceArray = $Voice->where($condition)->select();
        if (count($voiceArray) == 0) {
            $voiceArray = array();
        } else {
            for ($index = 0; $index < count($voiceArray); $index++) {
                $voiceArray[$index]['UploadTime'] = date('Y-m-d H:i:s', $voiceArray[$index]['UploadTime']);
            }
        }
        $json = json_encode($voiceArray, JSON_UNESCAPED_UNICODE);
        $this->ajaxReturn($json, 'EVAL');
    }

    // </editor-fold>
    // <editor-fold defaultstate="collapsed" desc="视频信息">
    private function SaveVideoInfo($info) {
        $Video = M('noticvideo');
//        $VideoData['Title'] = $info['title'];
//        $VideoData['Summary'] = $info['description'];
        $VideoData['FileName'] = $info['name'];
        $VideoData['FilePath'] = "Uploads/Videos/" . $info['savepath'] . $info['savename'];
        $VideoData['UploadTime'] = time();
        $VideoData['PublicCodeID'] = 1;
        $result = $Video->add($VideoData);
        return $result;
    }

    //更新视频信息中的标题和内容
    public function UpdateVideoInfo() {
        $id = I('id', '', 'htmlspecialchars');
        $title = I('title', '', 'htmlspecialchars');
        $description = I('description', '', 'htmlspecialchars');
        if ($id != 0) {
            $Video = M('noticvideo');
            $data = array('Title' => $title, 'Description' => $description);
            $Video->where('ID=' . $id)->setField($data);
            $this->ajaxReturn('y', 'EVAL');
        }
        $this->ajaxReturn('n', 'EVAL');
    }

    /**
     * 上传图文信息的封面
     * @param type $info
     * @return type
     */
    private function SaveGraphicimgInfo($info) {
        $Graphic = M('noticegraphic');
        $GraphicData['CoverPath'] = "Uploads/Graphics/" . $info['savepath'] . $info['savename'];
        $GraphicData['UploadTime'] = time();
        $GraphicData['PublicCodeID'] = 1;
        $addID = $Graphic->add($GraphicData);
        $result = array("ID" => $addID, "imgpath" => $GraphicData['CoverPath']);
        return $result;
    }

    //更新单图文信息
    public function UpdateGraphicInfo() {
        $title = I('graphictitle', '', 'htmlspecialchars');
        $author = I('graphicauthor', '', 'htmlspecialchars');
        $mainbody = I('mainbody', '', 'htmlspecialchars');
        $id = I('graphicid', '', 'htmlspecialchars');
        $ifShow = I('ifshow', '', 'htmlspecialchars');
        $summary = I('summary', '', 'htmlspecialchars');
        $originalUrl = I('originalurl', '', 'htmlspecialchars');
        $graphictype = 0;

//        echo " 这里是前台的消息 start==> author=" . $author 
//                . " title=" . $title 
//                . " mainbody= " . $mainbody 
//                . " digest= " . $summary 
//                . " show_cover_pic =" . $originalUrl 
//                . "<==end";
        /**
         * 判断标题和正文内容是否为空
         */
        if ($title == null || $title == "") {
            $this->ajaxReturn('titleempty', 'EVAL');
            return;
        } else if ($mainbody == null || $mainbody == "") {
            $this->ajaxReturn('mainbodyempty', 'EVAL');
            return;
        }
//        else if ($summary == null || $summary == "") {
//            $this->ajaxReturn('summaryempty', 'EVAL');
//            return;
//        }

        $Graphic = M("noticegraphic"); //区分大小写
        $data = array("GraphicTitle" => $title, "GraphicAuthor" => $author, "MainBody" => $mainbody,
            "ID" => $id, "IfShow" => $ifShow, "Summary" => $summary, "OriginalUrl" => $originalUrl,
            "GraphicType" => $graphictype, "GraphicOrder" => 1);
        $result = $Graphic->save($data);
        if ($result > 0) {
            $this->ajaxReturn('y', 'EVAL');
        } else {
            $this->ajaxReturn('n', 'EVAL');
        }
    }

    //更新多图文信息
    public function UpdateMpNewsInfo() {
        $mpnewsList = I('mpnewsList', '', 'htmlspecialchars');
        $partentid = $this->getRandNum();
        $title;
        $author;
        $mainbody;
        $id;
        $ifShow;
        $originalUrl;
        $graphicorder;
        foreach ($mpnewsList as $item) {
            foreach ($item as $key => $value) {
                switch ($key) {
                    case "graphictitle":
                        $title = $value;
                        break;
                    case "graphicauthor":
                        $author = $value;
                        break;
                    case "mainbody":
                        $mainbody = $value;
                        break;
                    case "graphicid":
                        $id = $value;
                        break;
                    case "ifshow":
                        $ifShow = $value;
                        break;
                    case "originalurl":
                        $originalUrl = $value;
                        break;
                    case "graphicorder":
                        $graphicorder = $value;
                        break;
                }
            }
            $Graphic = M("noticegraphic"); //区分大小写
            $data = array("GraphicTitle" => $title, "GraphicAuthor" => $author, "MainBody" => $mainbody,
                "ID" => $id, "IfShow" => $ifShow, "OriginalUrl" => $originalUrl, "GraphicType" => 1,
                "ParentId" => $partentid, "GraphicOrder" => $graphicorder);
            $result = $Graphic->save($data);
        }
        if ($result > 0) {
            $this->ajaxReturn('y', 'EVAL');
        } else {
            $this->ajaxReturn('n', 'EVAL');
        }
    }

    public function getVideoList() {
        $Video = M('noticvideo');
        $condition['PublicCodeID'] = array('EQ', '1');
        $condition['Title'] = array('NEQ', '');
        $videoArray = $Video->where($condition)->order('UploadTime')->select();
        if (count($videoArray) == 0) {
            $videoArray = array();
        } else {
            for ($index = 0; $index < count($videoArray); $index++) {
                $videoArray[$index]['UploadTime'] = date('Y-m-d H:i:s', $videoArray[$index]['UploadTime']);
            }
        }
        $json = json_encode($videoArray, JSON_UNESCAPED_UNICODE);
        $this->ajaxReturn($json, 'EVAL');
    }

    /**
     * 获取图文列表
     */
    public function getGraphicList() {
        $Graphic = M('noticegraphic');
        $condition['PublicCodeID'] = array('EQ', '1');
        $condition['GraphicOrder'] = array('EQ', '1');
        $condition['GraphicTitle'] = array('NEQ', '');
        $graphicArray = $Graphic->where($condition)->select();
        if (count($graphicArray) == 0) {
            $graphicArray = array();
        } else {
            for ($index = 0; $index < count($graphicArray); $index++) {
                $graphicArray[$index]['UploadTime'] = date('Y-m-d H:i:s', $graphicArray[$index]['UploadTime']);
            }
        }
        $json = json_encode($graphicArray, JSON_UNESCAPED_UNICODE);
        $this->ajaxReturn($json, 'EVAL');
    }

    /**
     * 获取某一条多图文信息的具体内容
     */
    public function getMulMpList() {
        $parentid = I('parentid', '', 'htmlspecialchars');
        $Graphic = M('noticegraphic');
        $condition['PublicCodeID'] = array('EQ', '1');
        $condition['GraphicTitle'] = array('NEQ', '');
        $condition['ParentId'] = array('EQ', $parentid);

        $graphicArray = $Graphic->where($condition)->select();
        if (count($graphicArray) == 0) {
            $graphicArray = array();
        } else {
            for ($index = 0; $index < count($graphicArray); $index++) {
                $graphicArray[$index]['UploadTime'] = date('Y-m-d H:i:s', $graphicArray[$index]['UploadTime']);
            }
        }
        $json = json_encode($graphicArray, JSON_UNESCAPED_UNICODE);
        $this->ajaxReturn($json, 'EVAL');
    }

    /**
     * 获取随机数
     * @return type
     */
    public function getRandNum() {
        $rdnum = mt_rand(10, 1000000);
        while ($this->getCountFromMysql($rdnum) > 0) {
            $rdnum = mt_rand(10, 1000000);
        }
        return __RDNUMHEAD__ . $rdnum;
    }

    /**
     * 查询随机数是否已存在
     * @param type $rdnum
     * @return type
     */
    public function getCountFromMysql($rdnum) {
        $Graphic = M('noticegraphic');
        $condition["ParentId"] = __RDNUMHEAD__ . $rdnum;
        $rtcount = $Graphic->where($condition)->count();

        return $rtcount;
    }

    // </editor-fold>
    //列表消息
    public function saveListNotice() {
        $listNoticeObj = M('listnotice');
        $title = I('title', '', 'htmlspecialchars');
        $htmlData = I('htmlData');
        $NoticeID = I('NoticeID', '', 'htmlspecialchars');
        $htmlData = htmlspecialchars_decode($htmlData);
        $clean_HTML = HTMLFilter($htmlData);
        //////////////////////////////////////////////////////////
        //Edit by Ron.shen 此处经过防XSS处理过后存入mysql出现了换行和空格不能存入，数据库中显示为？，但是HTML中标签中又可能会存在空格，所以
        //                  不能全部替换空格，试验以后发现需要单独修改数据表中该列编码为utf8_unciode_ci，所以此处附上修改单列编码的SQL语句
        //                  ALTER TABLE  `sh_listnotice` CHANGE  `NoticHTMLContent`  `NoticHTMLContent` TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL
        //////////////////////////////////////////////////////////
        $clean_HTML = str_replace("<em>", "<i>", $clean_HTML);
        $clean_HTML = str_replace("</em>", "</i>", $clean_HTML);

        if ($NoticeID != '') {
            $listNoticeData['ID'] = $NoticeID;
        }
        $listNoticeData['Title'] = $title;
        $listNoticeData['NoticHTMLContent'] = $clean_HTML;
        $listNoticeData['UpdateTime'] = time();
        $listNoticeData['PublicCodeID'] = 1;

        $ID = $listNoticeObj->add($listNoticeData, array(), true);
        $result = array();
        if ($ID) {
            $result['error'] = 0;
            $result['ID'] = $ID;
        } else {
            $result['error'] = 1;
        }
        $json = json_encode($result, JSON_UNESCAPED_UNICODE);
        $this->ajaxReturn($json, 'EVAL');
    }

    public function getListNotice() {
        $listNoticeObj = M('listnotice');
        $condition['PublicCodeID'] = 1;
        $listNoticeArray = $listNoticeObj->where($condition)->select();
        if ($listNoticeArray) {
            for ($index = 0; $index < count($listNoticeArray); $index++) {
                $listNoticeArray[$index]['UpdateTime'] = date('Y-m-d H:i:s', $listNoticeArray[$index]['UpdateTime']);
            }
            $json = json_encode($listNoticeArray, JSON_UNESCAPED_UNICODE);
            $this->ajaxReturn($json, 'EVAL');
        } else {
            $this->ajaxReturn('[]', 'EVAL');
        }
    }

    public function deleteNotice() {
        $NoticeID = I('NoticeID', '', 'htmlspecialchars');
        $noticeObj = M('listnotice');
        $condition["PublicCodeID"] = 1;
        $condition["ID"] = $NoticeID;
        $noticeData = $noticeObj->where($condition)->delete();
        if ($noticeData) {
            $this->ajaxReturn('y', 'EVAL');
        } else {
            $this->ajaxReturn('n', 'EVAL');
        }
    }

}
